package com.toe.user.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.restful.JsonPage;
import com.toe.pojo.dto.user.UserPermissionAddDTO;
import com.toe.pojo.dto.user.UserPermissionUpdateDTO;
import com.toe.pojo.entity.user.UserPermission;
import com.toe.pojo.vo.user.UserPermissionVO;
import com.toe.user.webapi.mapper.UserPermissionMapper;
import com.toe.user.service.UserPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.user.webapi.mapper.UserRolePermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class UserPermissionServiceImpl extends ServiceImpl<UserPermissionMapper, UserPermission> implements UserPermissionService {


    @Autowired
    private UserPermissionMapper userPermissionMapper;

    @Autowired
    private UserRolePermissionMapper userRolePermissionMapper;

    @Override
    public void insert(UserPermissionAddDTO userPermissionAddDTO) {
        log.info("开始添加权限业务");
        UserPermission userPermission = new UserPermission();
        BeanUtils.copyProperties(userPermissionAddDTO,userPermission);
        LocalDateTime date = LocalDateTime.now();
        userPermission.setUpdateTime(date);
        userPermission.setCreateTime(date);
        userPermissionMapper.insert(userPermission);
    }

    @Override
    public void delete(long id) {
        log.info("开始删除权限业务");
        userPermissionMapper.deleteById(id);
        userRolePermissionMapper.deleteByPermissionId(id);
    }

    @Override
    public List<UserPermissionVO> selectAllPermission() {
        List<UserPermissionVO> userPermissionVOS = userPermissionMapper.selectAllPermission();
        return userPermissionVOS;
    }

    @Override
    public JsonPage<UserPermissionVO> selectAllPermissionPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserPermissionVO> userPermissionVOS = userPermissionMapper.selectAllPermission();
        return JsonPage.restPage(new PageInfo<>(userPermissionVOS));
    }

    @Override
    public void updatePermission(UserPermissionUpdateDTO userPermissionUpdateDTO) {
        userPermissionMapper.updatePermission(userPermissionUpdateDTO);
    }
}
